BX.CrmEntitySelector=function(){var t=function(t){this.randomString=t.randomString;this.jsObject=t.jsObject;this.fieldUid=t.fieldUid;this.fieldName=t.fieldName;this.usePrefix=t.usePrefix;this.listPrefix=t.listPrefix;this.selectorEntityTypes=t.selectorEntityTypes;this.multiple=t.multiple;this.listElement=t.listElement;this.listEntityType=t.listEntityType;this.pluralCreation=Boolean(t.pluralCreation);this.listEntityCreateUrl=t.listEntityCreateUrl;this.currentEntityType=t.currentEntityType;this.context=t.context;this.initialize()};t.prototype.initialize=function(){this.popupObject=null;this.popupId="crm-"+this.randomString+"-popup";this.popupBindElement=null;this.popupContent="";this.externalRequestData=null;this.externalEventHandler=null;BX.addCustomEvent("onCrmSelectedItem",BX.proxy(this.setSelectedElement,this));BX.addCustomEvent("onCrmUnSelectedItem",BX.proxy(this.unsetSelectedElement,this))};t.prototype.createNewEntity=function(t){if(this.pluralCreation){t=t||window.event;this.popupBindElement=t.currentTarget;this.createPopup()}else{this.performExternalRequest()}};t.prototype.performExternalRequest=function(t){if(this.popupObject){this.popupObject.popupWindow.close()}if(t){this.setCurrentEntityType(t)}var e=BX.util.add_url_param(this.getCreateUrl(),{external_context:this.context});BX.SidePanel.Instance.open(e);if(!this.externalRequestData){this.externalRequestData={}}this.externalRequestData[this.context]={context:this.context,url:e};if(!this.externalEventHandler){this.externalEventHandler=BX.delegate(this.onExternalEvent,this);BX.addCustomEvent(window,"onLocalStorageSet",this.externalEventHandler)}};t.prototype.onExternalEvent=function(t){var e=BX.type.isNotEmptyString(t["key"])?t["key"]:"";var i=BX.type.isPlainObject(t["value"])?t["value"]:{};var s=BX.type.isNotEmptyString(i["entityTypeName"])?i["entityTypeName"]:"";var n=BX.type.isNotEmptyString(i["context"])?i["context"]:"";if(e==="onCrmEntityCreate"&&s===this.currentEntityType.toUpperCase()&&this.externalRequestData&&BX.type.isPlainObject(this.externalRequestData[n])){var r=BX.type.isBoolean(i["isCanceled"])?i["isCanceled"]:false;if(!r&&BX.type.isPlainObject(i["entityInfo"])){var l=BX.UI.SelectorManager.instances[this.fieldUid];var a=i.entityInfo.type.toUpperCase();if(BX.type.isNotEmptyObject(l)){var o={};if(this.multiple=="Y"){for(var p in l.itemsSelected){if(!l.itemsSelected.hasOwnProperty(p)){continue}o[p]=l.itemsSelected[p]}}o[this.listPrefix[a]+"_"+i.entityInfo.id]=this.selectorEntityTypes[a];BX.onCustomEvent("BX.Main.SelectorV2:reInitDialog",[{selectorId:this.fieldUid,selectedItems:o}])}}if(this.externalRequestData[n]["url"]){var c=BX.SidePanel.Instance.getSlider(this.externalRequestData[n]["url"]);if(c){c.destroy()}}delete this.externalRequestData[n]}};t.prototype.createPopup=function(){var t=[];for(var e=0;e<this.listEntityType.length;e++){t.push({text:BX.message("CRM_CES_CREATE_"+this.listEntityType[e].toUpperCase()),onclick:'BX["'+this.jsObject+'"].performExternalRequest("'+this.listEntityType[e]+'");'})}if(!BX.PopupMenu.getMenuById(this.popupId)){var i=this.popupBindElement.getBoundingClientRect();this.popupObject=BX.PopupMenu.create(this.popupId,this.popupBindElement,t,{closeByEsc:true,angle:true,offsetLeft:i.width/2})}if(this.popupObject){this.popupObject.popupWindow.show()}};t.prototype.setCurrentEntityType=function(t){this.currentEntityType=t};t.prototype.getCreateUrl=function(){if(this.listEntityCreateUrl.hasOwnProperty(this.currentEntityType)){return this.listEntityCreateUrl[this.currentEntityType]}else{return""}};t.prototype.setSelectedElement=function(t){for(var e in this.listElement){if(t.id===this.listElement[e].id){this.listElement[e].selected="Y"}}};t.prototype.unsetSelectedElement=function(t){for(var e in this.listElement){if(t.id===this.listElement[e].id){this.listElement[e].selected="N"}}};t.prototype.initWidgetEntitySelection=function(){BX.loadCSS("/bitrix/js/crm/css/crm.css");if(typeof CRM=="undefined"){BX.loadScript("/bitrix/js/crm/crm.js",BX[""+this.jsObject+""].initWidgetEntitySelection());return}CRM.Set(BX("crm-"+this.fieldUid+"-open"),this.fieldName,"",this.listElement,this.usePrefix==="Y",this.multiple==="Y",this.listEntityType,{lead:BX.message("CRM_FF_LEAD"),contact:BX.message("CRM_FF_CONTACT"),company:BX.message("CRM_FF_COMPANY"),deal:BX.message("CRM_FF_DEAL"),quote:BX.message("CRM_FF_QUOTE"),order:BX.message("CRM_FF_ORDER"),ok:BX.message("CRM_FF_OK"),cancel:BX.message("CRM_FF_CANCEL"),close:BX.message("CRM_FF_CLOSE"),wait:BX.message("CRM_FF_SEARCH"),noresult:BX.message("CRM_FF_NO_RESULT"),add:BX.message("CRM_FF_CHOISE"),edit:BX.message("CRM_FF_CHANGE"),search:BX.message("CRM_FF_SEARCH"),last:BX.message("CRM_FF_LAST")})};return t}();
//# sourceMappingURL=script.map.js